/*
 * Copyright (C) 2013 gujicheng
 * 
 * Licensed under the GPL License Version 2.0;
 * you may not use this file except in compliance with the License.
 * 
 * If you have any question, please contact me.
 * 
 *************************************************************************
 **                   Author information                                **
 *************************************************************************
 ** Email: gujicheng197@126.com                                         **
 ** QQ   : 29600731                                                     **
 ** Weibo: http://weibo.com/gujicheng197                                **
 *************************************************************************
 */
package com.libra.sinvoice

import com.libra.sinvoice.Buffer.BufferData

/**
 *
 * @ClassName: com.libra.sinvoice.Encoder
 * @Description: 编码器
 * @author zhaokaiqiang
 * @date 2014-11-16 下午1:32:17
 */
class Encoder(private val encoderCallback: EncoderCallback?, sampleRate: Int, bits: Int,
              bufferSize: Int) : SinGenerator.SinGeneratorCallback {
    companion object {

        private val TAG = "Encoder"
        private val STATE_ENCODING = 1
        private val STATE_STOPED = 2

        // index 0, 1, 2, 3, 4, 5, 6
        // circleCount 31, 28, 25, 22, 19, 15, 10
        private var CODE_FREQUENCY = intArrayOf(1422, 1575, 1764, 2004, 2321, 2940, 4410)
        val maxCodeCount: Int
            get() = CODE_FREQUENCY.size
    }


    private var mState: Int = 0

    private val mSinGenerator: SinGenerator

    val isStoped: Boolean
        get() = STATE_STOPED == mState

    interface EncoderCallback {

        val encodeBuffer: BufferData?

        fun freeEncodeBuffer(buffer: BufferData)
    }

    init {
        mState = STATE_STOPED
        mSinGenerator = SinGenerator(this, sampleRate, bits, bufferSize)
        /*for (s in 1000..15000 step Common.FREQUENCY_STEP) {
            CODE_FREQUENCY.add(s)
        }*/
    }

    // content of input from 0 to (CODE_FREQUENCY.length-1)
    fun encode(codes: List<Int>, duration: Int) {
        if (STATE_STOPED == mState) {
            mState = STATE_ENCODING

            mSinGenerator.start()
            for (index in codes) {
                if (STATE_ENCODING == mState) {
                    if (index >= 0 && index < CODE_FREQUENCY.size) {
                        // 使用正弦发生器编码
                        mSinGenerator.gen(CODE_FREQUENCY[index], duration)
                    } else {
                        LogHelper.d(TAG, "code index error")
                    }
                } else {
                    LogHelper.d(TAG, "encode force stop")
                    break
                }
            }
            mSinGenerator.stop()

        }
    }

    fun stop() {
        if (STATE_ENCODING == mState) {
            mState = STATE_STOPED
            mSinGenerator.stop()
        }
    }



    override fun getGenBuffer(): BufferData? {
        return encoderCallback?.encodeBuffer
    }

    override fun freeGenBuffer(buffer: BufferData) {
        encoderCallback?.freeEncodeBuffer(buffer)
    }

}
